package com.sunjs.wechat.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sunjs.model.LyShoppingCart;
import com.sunjs.model.back.RestCode;
import com.sunjs.model.back.RestResponse;
import com.sunjs.wechat.service.WxShoppingCartService;

import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/wx/shoppingcart")
public class WxShoppingCartController {
	
	@Autowired
	private WxShoppingCartService  wxShoppingCartService;
	
//	新增购物车
//	{
//	    "productId":1,
//	    "normList":[
//	        0,1,2
//	        ],
//	        "createUser":"1000220006",
//	        "total":1,
//	        "userloginId":1
//	}
	//返回消息需要统一改下
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public RestResponse<Map<String, Object>> insert(@RequestBody LyShoppingCart lyShoppingCart) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject jsonObject = wxShoppingCartService.insert(lyShoppingCart);
			String code = (String) jsonObject.get("code");
			String msg = (String)jsonObject.get("msg");
			map.put("code", code);
			map.put("msg", msg);
		    if(code.equals("1")){
		    	return RestResponse.checkError(1,msg);
		    }
			return RestResponse.success(map);
		} catch (Exception e) {
			return RestResponse.error(RestCode.FAIL);
		}
	}

	//	{
//	    "createUser": "1000220006",
//	    "userloginId": "1000000003"
//	}
//	返回值
//	  "normNameList": [
//	                   {
//	                       "id": null,
//	                       "productCategoryId": null,
//	   规格名称显示到页面上的                    "value": "128G",
//	                       "name": null
//	                   },
//	                   {
//	                       "id": null,
//	                       "productCategoryId": null,
//	     规格名称显示到页面上的                       "value": "256G",
//	                       "name": null
//	                   }
//	               ],
	/* 查询当前用户的购物车列表 */
	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public RestResponse<List<LyShoppingCart>> select(@RequestBody LyShoppingCart lyShoppingCart) {
		try {
			List<LyShoppingCart> pair = wxShoppingCartService.select(lyShoppingCart);
			return  RestResponse.success(pair);
		} catch (Exception e) {
			return RestResponse.error(RestCode.FAIL);
		}
	}
	
	/* 购物车加减 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public RestResponse<Map<String, Object>> update(@RequestBody LyShoppingCart lyShoppingCart) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			JSONObject jsonObject = wxShoppingCartService.update(lyShoppingCart);
			String code = (String) jsonObject.get("code");
			String msg = (String)jsonObject.get("msg");
			map.put("code", code);
			map.put("msg", msg);
		    if(code.equals("1")){
		    	return RestResponse.checkError(1,msg);
		    }
			return RestResponse.success(map);
		} catch (Exception e) {
			
			
			return RestResponse.error(RestCode.FAIL);
		}
	}
	
	/* 购物车删除*/
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public RestResponse<Integer> delete(@RequestBody LyShoppingCart lyShoppingCart) {
		try {
			int count = wxShoppingCartService.delete(lyShoppingCart);
		    if(count<1){
		    	return RestResponse.error(RestCode.FAIL);
		    }
			return RestResponse.success(count);
		} catch (Exception e) {
			return RestResponse.error(RestCode.FAIL);
		}
	}
	
}


